$(function(){
       $('#dg').datagrid({
           view: detailview,
		   pageSize:20,
		   idField:'id',	
           detailFormatter:function(index,row){
               return '<div class="ddv"></div>';
           },
           onExpandRow: function(index,row){
               var ddv = $(this).datagrid('getRowDetail',index).find('div.ddv');
               ddv.panel({
                   border:false,
                   cache:true,
                   href:'admin.php?r=index/showGoodForm&index='+index,
                   onLoad:function(){
                       $('#dg').datagrid('fixDetailRowHeight',index);
                       $('#dg').datagrid('selectRow',index);
                       $('#dg').datagrid('getRowDetail',index).find('form').form('load',row);
					   $('#dg').datagrid('getRowDetail',index).find('img').attr('src', row.pic_link);
                   }
               });
               $('#dg').datagrid('fixDetailRowHeight',index);
           }
       });
   });
   function saveItem(index){
       var row = $('#dg').datagrid('getRows')[index];
       var url = row.isNewRecord ? 'admin.php?r=index/saveProduct' : 'admin.php?r=index/updateProduct&id='+row.id;
       $('#dg').datagrid('getRowDetail',index).find('form').form('submit',{
           url: url,
           onSubmit: function(){
               return $(this).form('validate');
           },
           success: function(data){
               data = eval('('+data+')');
               data.isNewRecord = false;
               $('#dg').datagrid('collapseRow',index);
               $('#dg').datagrid('updateRow',{
                   index: index,
                   row: data
               });
           }
       });
   }
   function cancelItem(index){
       var row = $('#dg').datagrid('getRows')[index];
       if (row.isNewRecord){
           $('#dg').datagrid('deleteRow',index);
       } else {
           $('#dg').datagrid('collapseRow',index);
       }
   }
   function destroyItem(){
       var row = $('#dg').datagrid('getSelected');
       if (row){
           $.messager.confirm('Confirm','Are you sure you want to remove this user?',function(r){
               if (r){
                   var index = $('#dg').datagrid('getRowIndex',row);
                   $.post('admin.php?r=index/destoryProduct',{id:row.id},function(){
                       $('#dg').datagrid('deleteRow',index);
                   });
               }
           });
       }
   }
   function newItem(){
       $('#dg').datagrid('appendRow',{isNewRecord:true});
       var index = $('#dg').datagrid('getRows').length - 1;
       $('#dg').datagrid('expandRow', index);
       $('#dg').datagrid('selectRow', index);
   }
